package com.coocaa.app.lib.cleaner.deleter.pkgdata.impl;

import android.os.Environment;
import android.util.Log;

import com.coocaa.app.lib.cleaner.deleter.utils.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Eric on 2017/4/15.
 */

public class SpecialCleaner {

//    private static final String[] CLEAN_ARRAY = {"com.coocaa.mall","com.tianci.appstore","com.tianci.movieplatform"};

    private static final String CLEAN_MALL = "com.coocaa.mall";  //没有systemuid  删除不了
    private static final String CLEAN_APPSTORE = "com.tianci.appstore";
    private static final String CLEAN_MOVIEPALTFORM = "com.tianci.movieplatform";
    private static List<String> cleanList = new ArrayList<String>();

    static {
        cleanList.add(CLEAN_MALL);
        cleanList.add(CLEAN_APPSTORE);
        cleanList.add(CLEAN_MOVIEPALTFORM);
    }

    public static boolean needSpecialClean(String pkgName) {
        if (cleanList.contains(pkgName))
            return true;
        return false;
    }

    /**
     * @param pname
     * @return -1:清除失败  非-1：清除成功，并且有多少空间被删除
     */
    public static long deleteSpecialApplicationData(String pname) {
        if (pname.equals(CLEAN_MALL))
            return dMallData();
        else if (pname.equals(CLEAN_APPSTORE))
            return dAppstoreData();
        else if (pname.equals(CLEAN_MOVIEPALTFORM))
            return dMoviePlatform();
        return -1;
    }

    private static long dMallData() {
        return stardDelate(CLEAN_MALL);
    }

    private static long dAppstoreData() {
        return stardDelate(CLEAN_APPSTORE);
    }

    private static long dMoviePlatform() {
        try {
            long size = 0;
            List<String> list = new ArrayList<String>();
            String path1 = "data/data/com.tianci.movieplatform/cache";
            String path2 = "data/data/com.tianci.movieplatform/iqiyi_p2p";
            list.add(path1);
            list.add(path2);
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                String path3 = Environment.getExternalStorageDirectory() + "/iqiyi_p2p/";
                list.add(path3);
            }
            for (String path : list) {
                size += FileUtils.getFileSize(path);
                FileUtils.deleteFile(path);
            }
            Log.d("pclean","dMoviePlatform  ss:"+size);
            return size;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    private static long stardDelate(String pkg) {
        try {
            long size = 0;
            String root = "data/data/" + pkg + "/";
            File f = new File(root);
            if (f.isDirectory()) {
                String[] fileList = f.list();
                if (fileList != null && fileList.length > 0) {
                    for (int i = 0; i < fileList.length; i++) {
                        if (!fileList[i].equals("databases") && !fileList[i].equals("lib") && !fileList[i].equals("shared_prefs")) {
                            String path = f.getAbsolutePath() + "/" + fileList[i];
                            size += FileUtils.getFileSize(path);
                            FileUtils.deleteFile(path);
                        }
                    }
                }
            }
            Log.d("pclean","stardDelate pkg:"+pkg+" ss:"+size);
            return size;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

}
